// pages/good/detail/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "1",
    goodName: "b站七天大会员",
    goodDescription: "哔哩哔哩弹幕网七天大会员！",
    goodCarbonCurrency: "200",
    goodLeft: "10",
    imagePath: "../../../image/flower.png"
  },

  /**
   * 支付页面
   */
  toPay() {
    wx.navigateTo({
      url: '../pay/index?goodName=' + this.data.goodName + '&goodCarbonCurrency=' + this.data.goodCarbonCurrency,
    })
  },

  /**
   * 获取详情
   */
  getDetail(id) {
    var that = this

    wx.request({
      url: app.globalData.url + '/good/',
      method: "GET",
      data: {
        id: id
      },
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data[0])

        if(res.data.status_code == 500){
          wx.showToast({
            title: '获取失败',
            duration: 2000,
            icon: 'error'
          })
        }else {
          that.setData({
            goodName: res.data[0].goodName,
            goodDescription: res.data[0].goodDescription,
            goodCarbonCurrency: res.data[0].goodCarbonCurrency,
            goodLeft: res.data[0].goodLeft,
            imagePath: res.data[0].imagePath
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '连接失败',
          duration: 2000,
          icon: 'error',
        })
      }
    })
  },

  showConfirm() {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要兑换吗',
      success (res) {
        if (res.confirm) {
          that.exchange()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  exchange() {
    var that = this

    console.log(getApp().globalData.user.userId)
    console.log(that.data.id)
    console.log(that.data.goodCarbonCurrency)

    wx.request({
      url: app.globalData.url + "/good_exchange/",
      method: "POST",
      data: {
        userID: getApp().globalData.user.userId,
        goodID: that.data.id,
        goodCarbonCurrency: that.data.goodCarbonCurrency
      },
      header: {
        'Content-Type': 'application/json'
      },
      // 注册成功
      success: function (res) {

        console.log(res.data)

        if(res.data.status_code == 200){
          that.toPay()
        }else if(res.data.status_code == 500){
          wx.showToast({
            title: '碳币不足',
            icon: 'error',
            duration: 2000
          })
        }
        
      },
      fail: function(res){
        wx.showToast({
          title: '连接失败',
          icon: 'error',
          duration: 2000
        })

        // 测试
        that.toPay()
      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var id = options.id;

    // 从上一个页面获取餐品id
    _this.setData({
      id: id
    });

    console.log(_this.data.id)

    _this.getDetail(_this.data.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})